package com.cn.service.server.PartsManagement.Impl;

import com.cn.bean.pojo.lbj.ComponentCategory;
import com.cn.config.config.Result;
import com.cn.mapper.dao.db5.PartsManagement.ComponentCategoryDao;
import com.cn.service.server.PartsManagement.ComponentCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ComponentCategoryServiceImpl implements ComponentCategoryService {
    @Autowired(required = false)
    private ComponentCategoryDao categoryDao;
    @Override
    public List<ComponentCategory> selectComponentMechanicalUsageCategory(ComponentCategory category) {
        return categoryDao.selectComponentMechanicalUsageCategory(category);
    }

    @Override
    public Integer getComponentMechanicalUsageCategoryAutoid() {
        return categoryDao.getComponentMechanicalUsageCategoryAutoid();
    }

    @Override
    public Result addComponentMechanicalUsageCategory(String name, String description, int status) {
        Integer ID= categoryDao.getComponentMechanicalUsageCategoryAutoid();
        ID = ID != null ? ++ID : 1;
        String code = String.valueOf(ID);
        int i = categoryDao.addComponentMechanicalUsageCategory(ID, code, name, description, status);
        if (i == 0) {
            return Result.failure("500", "添加失败");
        }
        return Result.success("200", "添加成功");
    }
    @Override
    public Result delComponentMechanicalUsageCategoryStatus(int id) {
        int i = categoryDao.delComponentMechanicalUsageCategoryStatus(id);
        if (i > 0) {
            return Result.success("200", "删除成功");
        }
        return Result.failure("500", "删除失败");
    }

    @Override
    public Result updateComponentMechanicalUsageCategory(int id, String code, String name, String description, int status) {
        List<ComponentCategory> businessTypeTables = categoryDao.selectAll();

        for (ComponentCategory businessType : businessTypeTables) {
            // 如果状态为已删除，直接跳过
            if (businessType.getStatus() == -1) {
                continue;
            }
            // 如果不是当前记录，并且名称相同，则返回重复错误
            if (businessType.getId() != id && businessType.getName().equals(name)) {
                return Result.failure("500", "业务类型名称重复");
            }
        }
        int i = categoryDao.updateComponentMechanicalUsageCategory(id, code, name, description, status);

        if (i > 0) {
            return Result.success("200", "修改成功");
        }
        return Result.failure("500", "修改失败");
    }

    @Override
    public List<ComponentCategory> selectAllAuyo1() {
        return categoryDao.selectAllAuyo1();
    }
}
